package com.google.android.play.core.assetpacks;

import c.b.a.c.a.c.a;
import java.io.File;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;

final class o2 {

    /* renamed from: b  reason: collision with root package name */
    private static final a f2587b = new a("VerifySliceTaskHandler");

    /* renamed from: a  reason: collision with root package name */
    private final b0 f2588a;

    o2(b0 b0Var) {
        this.f2588a = b0Var;
    }

    private final void b(n2 n2Var, File file) {
        try {
            File w = this.f2588a.w(n2Var.f2586b, n2Var.f2581c, n2Var.f2582d, n2Var.e);
            if (w.exists()) {
                try {
                    if (w1.b(m2.a(file, w)).equals(n2Var.f)) {
                        f2587b.f("Verification of slice %s of pack %s successful.", n2Var.e, n2Var.f2586b);
                        return;
                    }
                    throw new v0(String.format("Verification failed for slice %s.", new Object[]{n2Var.e}), n2Var.f2585a);
                } catch (NoSuchAlgorithmException e) {
                    throw new v0("SHA256 algorithm not supported.", e, n2Var.f2585a);
                } catch (IOException e2) {
                    throw new v0(String.format("Could not digest file during verification for slice %s.", new Object[]{n2Var.e}), e2, n2Var.f2585a);
                }
            } else {
                throw new v0(String.format("Cannot find metadata files for slice %s.", new Object[]{n2Var.e}), n2Var.f2585a);
            }
        } catch (IOException e3) {
            throw new v0(String.format("Could not reconstruct slice archive during verification for slice %s.", new Object[]{n2Var.e}), e3, n2Var.f2585a);
        }
    }

    public final void a(n2 n2Var) {
        File d2 = this.f2588a.d(n2Var.f2586b, n2Var.f2581c, n2Var.f2582d, n2Var.e);
        if (d2.exists()) {
            b(n2Var, d2);
            File j = this.f2588a.j(n2Var.f2586b, n2Var.f2581c, n2Var.f2582d, n2Var.e);
            if (!j.exists()) {
                j.mkdirs();
            }
            if (!d2.renameTo(j)) {
                throw new v0(String.format("Failed to move slice %s after verification.", new Object[]{n2Var.e}), n2Var.f2585a);
            }
            return;
        }
        throw new v0(String.format("Cannot find unverified files for slice %s.", new Object[]{n2Var.e}), n2Var.f2585a);
    }
}
